<?php
function get_novel($url,$url2){
    function extraryTi($text) {
        preg_match('/[第\d\w+章]+\s+(.+)$/', $text, $matches);
        if (!empty($matches[1])) {
            return trim($matches[1]);
        } else {
            return null;
        }
    }
    function write_file($title,$content,$ml){
        $file = fopen('novel/'.$ml.'/'.$title.'.txt', "w");
        fwrite($file, $content);
        fclose($file);
        echo 'success';
    }
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $htmlContent = curl_exec($ch);
    if(curl_errno($ch)) {
        echo 'cURL Error: ' . curl_error($ch);
    }
    curl_close($ch);
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($htmlContent);
    libxml_clear_errors();
    $pTags = $dom->getElementsByTagName('p');
    $divTags = $dom->getElementsByTagName('h1');
    $extractedText = '';
    foreach ($pTags as $tag) {
        $extractedText .= $tag->nodeValue . ' ';
    }
    foreach ($divTags as $tag) {
        $extractedText2 = $tag->nodeValue . ' ';
    }
    write_file($extractedText2,trim($extractedText),$url2);
}
$url = $_GET['url'];
$url2 = $_GET['url2'];
get_novel($url,$url2);
?>